/**
 *  验证了以下内容
 *  1. sdns_sockaddr 对象间可以直接赋值
 *
 */
#include <iostream>
#include <netinet/in.h>
#include <arpa/inet.h>


union sdns_sockaddr {
    struct sockaddr_in in;
    struct sockaddr_in6 in6;
    struct sockaddr sa;
};

// 测试赋值
void test_assignment () {
    union sdns_sockaddr addr1;
    union sdns_sockaddr addr2;

    // 模拟填充 addr1 的值
    addr1.in.sin_family = AF_INET;
    addr1.in.sin_port = htons(8080);
    addr1.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

    // 将 addr1 的值赋给 addr2
    addr2 = addr1;

    std::cout << "addr2.in.sin_family: " << addr2.in.sin_family << std::endl;
    std::cout << "addr2.in.sin_port: " << ntohs(addr2.in.sin_port) << std::endl;
    std::cout << "addr2.in.sin_addr: " << ntohl(addr2.in.sin_addr.s_addr) << std::endl;

	{
		char ipStr[INET6_ADDRSTRLEN];
		if (addr2.sa.sa_family == AF_INET) {
			// IPv4
			inet_ntop(AF_INET, &(addr2.in.sin_addr), ipStr, INET6_ADDRSTRLEN);
		} else if (addr2.sa.sa_family == AF_INET6) {
			// IPv6
			inet_ntop(AF_INET6, &(addr2.in6.sin6_addr), ipStr, INET6_ADDRSTRLEN);
		} else {
			std::cout << "Unknown address family." << std::endl;
			return ;
		}
		std::cout << "addr2 ip=" << ipStr << " addr1 ip=" << INADDR_LOOPBACK << std::endl;
	}

}

int main() {
	test_assignment();
}
